import type { typeItem } from '../models/cateGoryModel';

export function flagToTree(data: typeItem[], id: number | null | undefined = 0): typeItem[] {
	const res = data.filter((item: typeItem) => item.pid == id);
	if (res.length === 0) return []; // 终止条件

	return res.map((child: typeItem) => {
		const children = flagToTree(data, child.id);
		return {
			...child,
			...(children.length > 0 && { children }),
		};
	});
}
